package v1

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"mycms/global"
	"mycms/model/common/response"
	"mycms/model/system"
	systemReq "mycms/model/system/request"
	systemRes "mycms/model/system/response"
	"mycms/service/admin"
	"mycms/service/common"
)


// @Tags 后台接口
// @Summary 获取用户动态路由
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param username formData string true "帐号"
// @Param password formData string true "密码"
// @Param captcha formData string true "验证码"
// @Param key_id formData string true "验证码ID"
// @Success 200 {object} systemRes.AdminLoginResponse
// @Router /admin/v1/login [post]
// @auth      xueneng
func (b *BaseApi)GetUserMenus (c *gin.Context) {
	menu := admin.GetUserMenus(0)
	response.OkWithDetailed(menu, "查询成功", c)
}


// @Tags 后台接口
// @Summary 后台菜单列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param title formData string true "路由标题"
// @Success 200 {object} systemRes.AdminLoginResponse
// @Router /admin/v1/menuList [post]
// @auth      xueneng
func (b *BaseApi)GetMenuTree (c *gin.Context) {
	var pageInfo systemReq.SearchMenuParams

	if ok := c.Bind(&pageInfo); ok != nil{
		//	返回错误信息
		response.FailWithMessage(ok.Error(), c)
		return
	}

	list := admin.GetMenuTree(0)

	if pageInfo.Type == "tree"{
		list = append([]systemRes.MenuListResponse{{
			Id:0,
			Title: "顶级菜单",
		}}, list...)
	}


	response.OkWithDetailed(response.PageResult{
		List:   list,
	}, "查询成功", c)


}

// @Tags 后台接口
// @Summary 后台菜单详情
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id formData string true "菜单ID"
// @Success 200 {object} systemRes.AdminLoginResponse
// @Router /admin/v1/menuInfo [post]
// @auth      xueneng
func (b *BaseApi)GetMenuInfo (c *gin.Context) {
	var pageInfo systemReq.MenuInfoParams

	if ok := c.Bind(&pageInfo); ok != nil{
		//	返回错误信息
		response.FailWithMessage(ok.Error(), c)
		return
	}

	errs,res := admin.GetMenuInfo(pageInfo)

	if errs != nil {
		global.LOG.Error("获取失败!", zap.Error(errs))
		response.FailWithMessage("获取失败！", c)
	} else {
		_, apis := admin.GetApis(res.ID)
		fmt.Printf("%+v", apis)
		response.OkWithDetailed(systemRes.MenuInfoResponse{
			Id:         res.ID,
			ParentId:   res.ParentId,
			Path:       res.Path,
			Icon:       res.Icon,
			Title:      res.Title,
			Component:  res.Component,
			AlwaysShow: res.AlwaysShow,
			Name:       res.Name,
			Status:     res.Status,
			Sort:       res.Sort,
			MenuType:   res.MenuType,
			Apis: 		apis,
		}, "查询成功", c)
	}
}

// @Tags 后台接口
// @Summary 编辑后台菜单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id formData string true "菜单ID"
// @Success 200 {object} systemRes.AdminLoginResponse
// @Router /admin/v1/menuUpdate [post]
// @auth      xueneng
func (b *BaseApi) UpdateMenu (c *gin.Context) {

	var param systemReq.UpdateMenuParams
	err := c.BindJSON(&param)

	if err != nil{
		//	返回错误信息
		response.FailWithMessage(err.Error(), c)
		return
	}

	//	验证参数是否合法
	if valErr := common.Validate.Struct(param); valErr != nil{
		response.FailWithMessage(common.Translate(valErr, param), c)
		return
	}

	errs := admin.UpdateMenu(param)

	if errs != nil {
		global.LOG.Error("保存失败!", zap.Error(errs))
		response.FailWithMessage( "保存失败！", c)
	} else {
		response.OkWithDetailed("", "保存成功", c)
	}
}


// @Tags 后台接口
// @Summary 添加后台菜单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id formData string true "菜单ID"
// @Success 200 {object} systemRes.AdminLoginResponse
// @Router /admin/v1/menuCreate [post]
// @auth      xueneng
func (b *BaseApi)CreateMenu(c *gin.Context)  {
	var param systemReq.CreateMenuParams
	err := c.BindJSON(&param)

	if err != nil{
		//	返回错误信息
		response.FailWithMessage(err.Error(), c)
		return
	}

	//	验证参数是否合法
	if valErr := common.Validate.Struct(param); valErr != nil{
		response.FailWithMessage(common.Translate(valErr, param), c)
		return
	}

	menu := &system.Menu{
		Title: param.Title,
		Name: param.Name,
		ParentId: param.ParentId,
		Path: param.Path,
		Status: param.Status,
		Sort: param.Sort,
		AlwaysShow: param.AlwaysShow,
		Icon: param.Icon,
		Component: param.Component,
		MenuType: param.MenuType,
	}

	errs := admin.CreateMenu(*menu, param.ApiId)
	if errs != nil {
		global.LOG.Error("新增失败!", zap.Error(errs))
		response.FailWithDetailed("", "新增失败："+errs.Error(), c)
	} else {
		response.OkWithDetailed("", "新增成功", c)
	}
}

// @Tags 后台接口
// @Summary 删除后台菜单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id formData string true "菜单ID"
// @Success 200 {object} systemRes.AdminLoginResponse
// @Router /admin/v1/menuDel [post]
// @auth      xueneng
func (b *BaseApi)DelMenu(c *gin.Context) {
	var param systemReq.MenuDelParams

	err := c.Bind(&param)

	if err != nil{
		//	返回错误信息
		response.FailWithMessage(err.Error(), c)
		return
	}

	//	验证参数是否合法
	if valErr := common.Validate.Struct(param); valErr != nil{
		response.FailWithMessage(common.Translate(valErr, param), c)
		return
	}

	errs := admin.DelMenu(param)

	if errs != nil {
		global.LOG.Error("删除失败!", zap.Error(errs))
		response.FailWithDetailed("", "删除失败："+errs.Error(), c)
	} else {
		response.OkWithDetailed("", "删除成功", c)
	}
}

